package  {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.display.SimpleButton;
	import flash.display.LoaderInfo;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	import org.silex.wysiwyg.toolboxApi.ToolBoxAPIController;
	import org.silex.wysiwyg.toolboxApi.event.ToolBoxAPIEvent;
	import org.silex.adminApi.SilexAdminApi;
	import org.silex.adminApi.listedObjects.Component;
	import org.silex.adminApi.listedObjects.Property;
	
	public class OpenPixlr extends Sprite {

		public var button:SimpleButton;
		
		public function OpenPixlr() 
		{
			button = new buttonDown();
			button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			addChild(button);
		}
		private function mouseDownHandler(event:MouseEvent):void
		{
//			ToolBoxAPIController.getInstance().setAsDefault(pluginUrl, "plugin");
//			ToolBoxAPIController.getInstance().loadEditor(pluginUrl, "plugin");
			var components:Array = SilexAdminApi.getInstance().components.getData()[0];
			//retrieve the selected components
			var selectedComponents:Array = getSelectedComponents(components);
			
			if (selectedComponents.length > 0 )
			{
				// retrieve the 1st component
				var selectedComponent:Component = selectedComponents[0];
				//
				var properties:Array = SilexAdminApi.getInstance().properties.getData([selectedComponent.uid])[0];
				var property:Property = getProperty(properties,"url");
				var rootUrl:String = LoaderInfo(this.parent.loaderInfo).parameters.rootUrl;
				var session_id:String = LoaderInfo(this.loaderInfo).parameters.session_id;
				
				// compute filen name and folder
/**/				var fSlash: int = property.currentValue.lastIndexOf("/");
				var bSlash: int = property.currentValue.lastIndexOf("\\"); // reason for the double slash is just to escape the slash so it doesn't escape the quote!!!
				var slashIndex: int = fSlash > bSlash ? fSlash : bSlash;

				var destFolder:String = property.currentValue.substr(0,slashIndex);
				var destFileName:String = property.currentValue.substr(slashIndex + 1);
/**/
				

//				var url:String = "http://pixlr.com/express/";
//				var url:String = "http://pixlr.com/editor/";
				var url:String = "http://pixlr.com/"+LoaderInfo(this.loaderInfo).parameters.pixlerMode+"/";
				var variables:URLVariables = new URLVariables();
				var request:URLRequest = new URLRequest(url);
				variables.s = "c"
				variables.target = rootUrl+"plugins/pixlr/uploadPixlrImage.php?session_id="+session_id+"&folder="+destFolder+"&name="+destFileName; 
				variables.exit = "javascript:window.close()"; 
				variables.locktype = "source";
				variables.locktitle = true;
				variables.credentials = true;
				variables.method = "GET";
				variables.referrer = "Silex";
				variables.image = rootUrl+property.currentValue;
				variables.title = getProperty(properties,"url").currentValue;
				request.method = "GET";//"POST";//
				request.data = variables;
				try {
					navigateToURL(request,"_blank");
				}
				catch (e:Error) {
					// handle error here
				}
			}
		}
		/**
		 * returns the selected component among an array of components
		 * 
		 * @param components the array of components to search
		 */ 
		private function getProperty(properties:Array,  propertyName:String):Property
		{
			for (var i:int = 0; i<properties.length; i++)
			{
				if (properties[i].name == propertyName)
				{
					return properties[i];
				}
			}
			return null;
		}
		/**
		 * returns the selected component among an array of components
		 * 
		 * @param components the array of components to search
		 */ 
		private function getSelectedComponents(components:Array):Array
		{
			var selectedComponentsUid:Array = SilexAdminApi.getInstance().components.getSelection();
			var selectedComponents:Array = new Array();
			
			for (var i:int = 0; i<components.length; i++)
			{
				for (var j:int = 0; j<selectedComponentsUid.length; j++)
				{
					if (components[i].uid == selectedComponentsUid[j])
					{
						selectedComponents.push(components[i]);
					}
				}
				
			}
			
			return selectedComponents;
		}
	}
	
}
